package com.example.timmy.myapplication.utils;

import android.graphics.Paint;
import android.graphics.Rect;
import android.util.TypedValue;

import com.example.timmy.myapplication.MyApplication;

/**
 * @Auther: Timmy
 * @Data: 2017/9/15
 * @Fuction:
 */

public class UiUtils {

    /**
     * dp 2 px
     *
     * @param dpVal
     */
    public static int dp2px(int dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, MyApplication.getContext().getResources().getDisplayMetrics());
    }


    public static int getTextHeight(Paint mPaint, String text) {
        Rect rect = new Rect();
        mPaint.getTextBounds(text, 0, text.length(), rect);
        return rect.height();
    }

    //3. 精确计算文字的宽度：
    public static int getTextWidth(Paint mPaint, String str) {
        float iSum = 0;
        if (str != null && !str.equals("")) {
            int len = str.length();
            float widths[] = new float[len];
            mPaint.getTextWidths(str, widths);
            for (int i = 0; i < len; i++) {
                iSum += Math.ceil(widths[i]);
            }
        }
        return (int) iSum;
    }
}
